package com.sitanyun.merchant.guide.push;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;

import com.blankj.utilcode.util.LogUtils;
import com.google.gson.Gson;
import com.sitanyun.merchant.guide.app.Contains;
import com.sitanyun.merchant.guide.bean.ForgetsmscoedeBean;
import com.sitanyun.merchant.guide.bean.MessagesBean;
import com.sitanyun.merchant.guide.callback.StringCallbacks;
import com.sitanyun.merchant.guide.url.Urls;
import com.sitanyun.merchant.guide.view.activity.SplashsActivity;
import com.sitanyun.merchant.guide.weiht.PhoneUtils;
import com.sitanyun.merchant.guide.weiht.SharedPreferenceUtil;
import com.zhy.http.okhttp.OkHttpUtils;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.jpush.android.api.CmdMessage;
import cn.jpush.android.api.CustomMessage;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.JPushMessage;
import cn.jpush.android.api.NotificationMessage;
import cn.jpush.android.service.JPushMessageReceiver;
import okhttp3.Call;
import okhttp3.MediaType;

/**
 * Created by jzl
 * on 2022/3/8.
 */
public class JGReceiver extends JPushMessageReceiver {

    private static final String TAG = "极光";

    /**
     * tag 增删查改的操作会在此方法中回调结果。
     * @param context
     * @param jPushMessage
     */
    @Override
    public void onTagOperatorResult(Context context, JPushMessage jPushMessage) {
        super.onTagOperatorResult(context, jPushMessage);
        EventBus.getDefault().post(jPushMessage);
    }

    /**
     * 查询某个 tag 与当前用户的绑定状态的操作会在此方法中回调结果。
     * @param context
     * @param jPushMessage
     */
    @Override
    public void onCheckTagOperatorResult(Context context, JPushMessage jPushMessage) {
        super.onCheckTagOperatorResult(context, jPushMessage);
        EventBus.getDefault().post(jPushMessage);
    }


    /**
     * alias 相关的操作会在此方法中回调结果。
     * @param context
     * @param jPushMessage
     */
    @Override
    public void onAliasOperatorResult(Context context, JPushMessage jPushMessage) {
        super.onAliasOperatorResult(context, jPushMessage);
        EventBus.getDefault().post(jPushMessage);
    }


    /**
     * 设置手机号码会在此方法中回调结果。
     * @param context
     * @param jPushMessage
     */
    @Override
    public void onMobileNumberOperatorResult(Context context, JPushMessage jPushMessage) {
        super.onMobileNumberOperatorResult(context, jPushMessage);
        EventBus.getDefault().post(jPushMessage);
    }


    /**
     * 注册成功回调
     * @param context
     * @param s
     */
    @Override
    public void onRegister(Context context, String s) {
        super.onRegister(context, s);
        LogUtils.e(TAG,"自带广播注册成功回调"+s);
    }

    /**
     * 长连接状态回调
     * @param context
     * @param b
     */
    @Override
    public void onConnected(Context context, boolean b) {
        super.onConnected(context, b);
        LogUtils.e(TAG,"自带广播长连接状态回调");
    }

    /**
     * 注册失败回调
     * @param context
     * @param cmdMessage
     */
    @Override
    public void onCommandResult(Context context, CmdMessage cmdMessage) {
        super.onCommandResult(context, cmdMessage);
        LogUtils.e(TAG,"自带广播注册失败回调"+cmdMessage.errorCode);
    }

    /**
     * 通知的MultiAction回调
     * @param context
     * @param intent
     */
    @Override
    public void onMultiActionClicked(Context context, Intent intent) {
        super.onMultiActionClicked(context, intent);
        LogUtils.e(TAG,"通知的MultiAction回调"+ "\n");

    }

    /**
     * 收到通知回调
     * @param context
     * @param notificationMessage
     */
    @Override
    public void onNotifyMessageArrived(Context context, NotificationMessage notificationMessage) {
        super.onNotifyMessageArrived(context, notificationMessage);
        LogUtils.e(TAG,"自带广播收到通知回调"+ "\n" + notificationMessage.toString());
        //通知ID
        int notificationId = notificationMessage.notificationId;
        if (Contains.ZHANDIANNAME.equals("前台")){
            Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
            vibrator.cancel();
            Log.d(TAG,Contains.ZHANDIANNAME);
            JPushInterface.clearNotificationById(context,notificationId);

        }
    }

    /**
     * 点击通知回调
     * @param context
     * @param notificationMessage
     */
    @Override
    public void onNotifyMessageOpened(Context context, NotificationMessage notificationMessage) {
        super.onNotifyMessageOpened(context, notificationMessage);
     //   LogUtils.e(TAG,"自带广播点击通知回调");
        Log.d("Opens", "JGReceiverJGReceiver用户点击打开了通知++@#!@3");
        String string=  notificationMessage.notificationExtras;
        if (string != null) {
            try {
                String remark=  new JSONObject(string).getString("remark");
              MessagesBean testBean=  new Gson().fromJson(remark, MessagesBean.class);
                if (testBean.getBehavior().equals("CALL")){
                    PhoneUtils.call(context, testBean.getCPhone());

                    initcallread(testBean.getNoticeId()+"");

                }else {
                    Intent i = new Intent(context, SplashsActivity.class);
                    Bundle bundle = new Bundle();
                    //  bundle.putString(JPushInterface.EXTRA_NOTIFICATION_TITLE,message.notificationTitle);
                    bundle.putString(JPushInterface.EXTRA_EXTRA, remark);
                    i.putExtras(bundle);
                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    context.startActivity(i);

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }

    private void initcallread( String s) {
        Map<String, List> hashMap = new HashMap<>();
        List lists = new ArrayList<>();
        lists.add(s);
        hashMap.put("list", lists);
        OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .addHeader("Content-Type", "application/json")
                .addHeader("Authorization", "Bearer " + SharedPreferenceUtil.getStringData("token"))
                .url(Urls.setIsRead)
                .content(new Gson().toJson(hashMap))
                .build().execute(new StringCallbacks() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {
                ForgetsmscoedeBean baseEntity = new Gson().fromJson(response, ForgetsmscoedeBean.class);
                if (baseEntity.getCode() == 0) {

                }
            }
        });
    }

    /**
     * 清除通知回调
     * @param context
     * @param notificationMessage
     */
    @Override
    public void onNotifyMessageDismiss(Context context, NotificationMessage notificationMessage) {
        super.onNotifyMessageDismiss(context, notificationMessage);
        LogUtils.e(TAG,"清除通知回调");

    }

    /**
     * 收到自定义消息回调
     * @param context
     * @param customMessage
     */
    @Override
    public void onMessage(Context context, CustomMessage customMessage) {
        super.onMessage(context, customMessage);
        LogUtils.e(TAG,"自带广播收到自定义消息回调");
    }

    /**
     *
     * @param context
     * @param isOn
     * @param source
     */
    @Override
    public void onNotificationSettingsCheck(Context context, boolean isOn, int source) {
        super.onNotificationSettingsCheck(context, isOn, source);
        Log.e(TAG, "[onNotificationSettingsCheck] isOn:" + isOn + ",source:" + source);

    }
}

